// pages/waiter/usercheck/usercheck.js
const request = require('@/utils/request.js')
const constants = require('@/utils/khyConstants.js')
const {debounce} = require('@/utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    order_bg: getApp().globalData.nginxIp+'/img/order_bg.png',
    qianshou_text: getApp().globalData.nginxIp+'/img/qianshou_text.jpg',
    scan_img: getApp().globalData.nginxIp+'/img/scan.jpg',
    sctp_img:getApp().globalData.nginxIp+'/img/sctp.png',
    packageStateList: getApp().globalData.dictData['packageState'],
    inputValue: '',
    timer: null,//定时任务
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  getPackageData(packageNo) {
    request.get('/UserController/queryOrderByPackageNo',{packageNo: packageNo}).then(res => {
      if (res.data.code == constants.resultSuccessCode) {
        if (res.data.result) {
          let data = res.data.result || []
          this.setData({
            dataList: data,
          })

        }
      }
    })
  },

  onScanAdd() {
    let that=this
    wx.scanCode({ //扫描API
        onlyFromCamera: true, //允许使用相册
        needResult: 1,
        scanType: ['barCode','qrCode'], //默认为二维码，barCode条码
        success: function (res) {
          if(res.result == ''){
            wx.showToast({
                title: '扫描失败',
                icon: 'error',
                duration: 2000
              })
          }else{
            that.setData({
              inputValue: res.result
            })
            that.getPackageData(res.result)
          }
        }
      })
  },

  orderinfo(e) {
    let order = e.currentTarget.dataset.order
    if (order) {
      getApp().globalData.pageData[constants.page_usercheckdetail] = order
      wx.navigateTo({
        url: '/pages/waiter/usercheckdetail/usercheckdetail',
      })
    }
  },

  clearHandle(e) {
    this.setData({
      inputValue: '',
      dataList: [],
    })
  },
  changeHandle(e) {
    const { value } = e.detail;
    let that=this
    this.setData({
      inputValue: value
    })
    
    if (value) {
      debounce(function() {
        that.getPackageData(value)
      }, 600)
    } else {
      setTimeout(() => {
        that.setData({
          dataList: [],
        })
      }, 600)
      
    }
    
  },


  record() {
    wx.navigateTo({
      url: '/pages/waiter/signHis/signHis',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.inputValue) {
      this.getPackageData(this.data.inputValue)
    } else {
      this.setData({
        dataList: [],
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})